package com.nirvana.ui;

import com.nirvana.MainPanel;
import com.nirvana.config.PluginConfig;
import com.nirvana.item.ResolvePropertyItem;

import javax.swing.*;

public class FilterFieldEditPanel {
    private JComponent defGenPanel;
    private JComponent entityGenPanel;
    private JComponent settingPanel;
    private JComponent filterFieldEditPanel;
    private JTextArea filterFieldTextArea;
    private JButton filterFieldCancelButton;
    private JButton tooltipSaveButton;
    private ResolvePropertyItem item;

    private static FilterFieldEditPanel INSTANCE;
    private FilterFieldEditPanel() {}

    public static FilterFieldEditPanel getInstance() {
        if (INSTANCE == null) {
            INSTANCE = new FilterFieldEditPanel();
        }
        return INSTANCE;
    }

    public void init(MainPanel mainPanel) {
        this.defGenPanel = mainPanel.configGeneratePanel;
        this.entityGenPanel = mainPanel.ormGeneratePanel;
        this.settingPanel = mainPanel.settingScrollPanel;
        this.filterFieldEditPanel = mainPanel.filterFieldEditPanel;
        this.filterFieldTextArea = mainPanel.filterFieldTextArea;
        this.filterFieldCancelButton = mainPanel.filterFieldCancelButton;
        this.tooltipSaveButton = mainPanel.filterFieldSaveButton;
        this.filterFieldCancelButton.addActionListener(e -> {
            close();
        });
        this.tooltipSaveButton.addActionListener(e -> {
            PluginConfig.getInstance().addFilterField(this.filterFieldTextArea.getText());
            close();
        });
    }

    private void close() {
        defGenPanel.setVisible(false);
        entityGenPanel.setVisible(false);
        settingPanel.setVisible(true);
        filterFieldEditPanel.setVisible(false);
    }

    public void open() {
        defGenPanel.setVisible(false);
        entityGenPanel.setVisible(false);
        settingPanel.setVisible(false);
        filterFieldEditPanel.setVisible(true);
        String filterField = String.join("\n", PluginConfig.getInstance().getIgnoreFields());
        filterFieldTextArea.setText(filterField);
    }
}
